package com.cosmos.mapdemo;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapController;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.OnGetPoiResultListener;
import com.tianditu.android.maps.Overlay;
import com.tianditu.android.maps.OverlayItem;
import com.tianditu.android.maps.PoiInfo;
import com.tianditu.android.maps.PoiSearch;

import java.util.ArrayList;
import java.util.List;


public class PoiSearchActivity extends ActionBarActivity implements OnGetPoiResultListener {

    private Context context = null;
    private MapView mapView = null;
    private EditText searchEditText = null;
    private Button searchButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poi_search);

        context = this;

        mapView = (MapView) findViewById(R.id.poi_mapView);
        mapView.setBuiltInZoomControls(true);
        MapController mapController = mapView.getController();
        GeoPoint point = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
        mapController.setCenter(point);
        mapController.setZoom(12);

        searchEditText = (EditText) findViewById(R.id.editText_search);
        searchButton = (Button) findViewById(R.id.button_search);
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchCondition = searchEditText.getText().toString().trim();
                if (searchCondition == null || "".equals(searchCondition)) {
                    Toast.makeText(PoiSearchActivity.this,
                            getString(R.string.toast_no_input_search),
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                PoiSearch poiSearch = new PoiSearch(context, PoiSearchActivity.this,
                        mapView);
                poiSearch.search(searchCondition, null, null);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_poi_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void OnGetPoiResult(ArrayList<PoiInfo> poiInfos,
                               ArrayList<PoiSearch.ProvinceInfo> provinceInfos,
                               String s, int error) {
        // 未找到具体的POI点
        if (poiInfos == null || poiInfos.size() == 0) {
            StringBuilder strBuilder = new StringBuilder();
            if (provinceInfos == null || provinceInfos.size() == 0) {
                strBuilder.append("没有找到结果");
            } else {
                // 列出存在POI信息的城市，可以再次调用搜索，在指定的城市内查找
                strBuilder.append("在");
                for (int i = 0; i < provinceInfos.size(); i++) {
                    PoiSearch.ProvinceInfo city = provinceInfos.get(i);
                    strBuilder.append(city.mStrName + ",");
                }
                strBuilder.append("找到结果");
            }
            Toast.makeText(context, strBuilder.toString(), Toast.LENGTH_SHORT).show();
            return;
        }

        List<Overlay> overlays = mapView.getOverlays();
        overlays.clear();
        Resources resources = getResources();
        Drawable marker = resources.getDrawable(R.drawable.tips_arrow);
        MultipleItemOverlay overlay = new MultipleItemOverlay(marker, context);
        // 处理获得的POI信息
        for (int i = 0; i < poiInfos.size(); i++) {
            PoiInfo poi = poiInfos.get(i);
            GeoPoint point = new GeoPoint((int) (poi.mDy * 1E6), (int) (poi.mDx * 1E6));
            overlay.addItem(new OverlayItem(point, poi.mStrName, Integer.toString(i)));
        }
        // 封装的通知接口，准备数据内容
        overlay.populateItemData();
        overlays.add(overlay);
        mapView.postInvalidate();
    }
}
